package top.zlcode.friend.service.question.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.zlcode.common.core.context.UserContextHelper;
import top.zlcode.friend.manager.UserFavoriteQuestionCacheManager;
import top.zlcode.friend.domain.question.UserFavoriteQuestion;
import top.zlcode.friend.domain.question.dto.FavoriteQuestionDTO;
import top.zlcode.friend.domain.question.dto.FavoriteQueryDTO;
import top.zlcode.friend.domain.question.vo.FavoriteQuestionVO;
import top.zlcode.friend.mapper.question.UserFavoriteQuestionMapper;
import top.zlcode.friend.service.question.IUserFavoriteQuestionService;

import java.util.List;


/**
 * 用户收藏题目Service实现
 * 
 * @author zlcode
 */
@Slf4j
@Service
public class UserFavoriteQuestionServiceImpl implements IUserFavoriteQuestionService {
    
    @Autowired
    private UserFavoriteQuestionMapper userFavoriteQuestionMapper;
    
    @Autowired
    private UserFavoriteQuestionCacheManager cacheManager;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean toggleFavorite(FavoriteQuestionDTO dto) {
        Long userId = UserContextHelper.getUserId();
        Long questionId = dto.getQuestionId();
        
        // 检查是否已收藏
        boolean isFavorite = checkFavorite(questionId);
        
        if (isFavorite) {
            // 取消收藏
            userFavoriteQuestionMapper.delete(
                new LambdaQueryWrapper<UserFavoriteQuestion>()
                    .eq(UserFavoriteQuestion::getUserId, userId)
                    .eq(UserFavoriteQuestion::getQuestionId, questionId)
            );
            cacheManager.removeFavorite(userId, questionId);
            return false;
        } else {
            // 添加收藏
            UserFavoriteQuestion favorite = new UserFavoriteQuestion();
            favorite.setUserId(userId);
            favorite.setQuestionId(questionId);

            userFavoriteQuestionMapper.insert(favorite);
            cacheManager.addFavorite(userId, questionId);
            return true;
        }
    }
    
    @Override
    public boolean checkFavorite(Long questionId) {
        Long userId = UserContextHelper.getUserId();
        
        // 先检查Redis key是否存在
        if (!cacheManager.hasCache(userId)) {
            // 如果Redis key不存在，刷新缓存
            cacheManager.refreshCache(userId);
        }
        
        // 直接走Redis逻辑判断是否收藏
        return cacheManager.isFavorite(userId, questionId);
    }
    
    @Override
    public List<FavoriteQuestionVO> getFavoriteList(FavoriteQueryDTO queryDTO) {
        Long userId = UserContextHelper.getUserId();
        // 使用PageHelper分页插件
        PageHelper.startPage(queryDTO.getPageNum(), queryDTO.getPageSize());
        return userFavoriteQuestionMapper.selectFavoriteQuestionsPage(userId, queryDTO.getKeyword());
    }
    
}
